package com.suxin.webflux.service;

import com.suxin.webflux.domain.User;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl {

    private List<User> list = Arrays.asList(
            new User(1,"name1",null,null),
            new User(2,"name2",null,null),
            new User(3,"name3",null,null)
    );

    public Mono<Void> save(Mono<User> userFlux) {

        return userFlux.doOnNext((item) -> {
            list.add(item);
        }).thenEmpty(Mono.empty());
    }

    public Mono<User> getId(Integer id) {
        return Mono.justOrEmpty(list.get(id));
    }

    public Flux<User> getList() {
        return Flux.fromIterable(this.list);
    }

}
